<template>
	<view v-if="goodsInfo.goods_name" class="goods-detail-container">
		<swiper indicator-dots autoplay duration="1000" circular interval="3000">
			<swiper-item v-for="(pic,i) in goodsInfo.pics" :key="pic.pics_id">
				<image :src="pic.pics_big" @click="preview(i)"></image>
			</swiper-item>
		</swiper>
		<!-- 商品信息区域 -->
		<view class="goods-info-box">
			<!-- 商品价格 -->
			<view class="price">￥{{goodsInfo.goods_price}}</view>
			<!-- 信息主体区域 -->
			<view class="goods-info-body">
				<!-- 商品名称 -->
				<view class="goods-name">{{goodsInfo.goods_name}}</view>
				<!-- 收藏 -->
				<view class="favi">
					<uni-icons type="star" size="18" color="gray"></uni-icons>
					<text>收藏</text>
				</view>
			</view>
			<!-- 运费 -->
			<view class="yf">快递：免运费</view>
		</view>
		<!-- 商品详情信息 -->
		<rich-text :nodes="goodsInfo.goods_introduce"></rich-text>
	</view>
	<!-- 商品导航 -->
	<view class="goods-carts">
		<uni-goods-nav :options="options" :fill="true" :button-group="buttonGroup" @click="onClick"
			@buttonClick="buttonClick" />
	</view>
</template>

<script setup>
	import {
		ref,
		reactive,
		watch,
		computed
	} from 'vue';
	import {
		onLoad
	} from '@dcloudio/uni-app';
	import {
		useaddToCartListStore
	} from '@/store/store.js';
	const addToCartListStore = useaddToCartListStore()

	onLoad((options) => {
		const goods_id = options.goods_id
		// console.log(goods_id);
		getgoodsInfo(goods_id)
	})
	const goodsInfo = reactive({})
	//获取商品详情
	const getgoodsInfo = async (goods_id) => {
		const {
			data: res
		} = await uni.$http.get('/api/public/v1/goods/detail', {
			goods_id
		})
		// console.log(res);
		if (res.meta.status !== 200) {
			return uni.$showMsg()
		}
		res.message.goods_introduce = res.message.goods_introduce.replace(/<img /g, '<img style="display:block;" ')
			.replace(/webp/g, 'jpg')
		Object.assign(goodsInfo, res.message)
		// console.log(goodsInfo);
	}
	//轮播图照片预览
	const preview = (index) => {
		uni.previewImage({
			current: index,
			urls: goodsInfo.pics.map(ele => ele.pics_big)
		})
	}
	// 定义商品导航数据
	const options = reactive([{
			icon: 'shop',
			text: '店铺',
			info: 0,
			infoBackgroundColor: '#007aff',
			infoColor: "#f5f5f5"
		},
		{
			icon: 'cart',
			text: '购物车',
			info: 0
		}
	]);

  //监听购物车数据，将购物车总数渲染到页面
  watch(()=>addToCartListStore.total, (newvalue) => {
		// console.log(newvalue);
  	options[1].info = newvalue
  }, {
  	immediate: true
  })

	const buttonGroup = ref([{
			text: '加入购物车',
			backgroundColor: 'linear-gradient(90deg, #FE6035, #EF1224)',
			color: '#fff'
		},
		{
			text: '立即购买',
			backgroundColor: 'linear-gradient(90deg, #FFCD1E, #FF8A18)',
			color: '#fff'
		}
	]);
	// 定义点击事件处理函数
	const onClick = (e) => {
		if (e.content.text === '购物车') {
			// 切换到购物车页面
			uni.switchTab({
				url: '/pages/cart/cart'
			})
		}
	};

	// 定义按钮组点击事件处理函数
	const buttonClick = (e) => {
		// console.log(e);
		// 1. 判断是否点击了 加入购物车 按钮
		if (e.content.text === '加入购物车') {
			// 2. 组织一个商品的信息对象
			const goods = {
				goods_id: goodsInfo.goods_id, // 商品的Id
				goods_name: goodsInfo.goods_name, // 商品的名称
				goods_price: goodsInfo.goods_price, // 商品的价格
				goods_count: 1, // 商品的数量
				goods_small_logo: goodsInfo.goods_small_logo, // 商品的图片
				goods_state: true // 商品的勾选状态
			}
			// 3. 通过仓库映射过来的 addToCart 方法，把商品信息对象存储到购物车中
			addToCartListStore.addToCartList(goods)
			// console.log(addToCartListStore.total);
		}
	};
</script>

<style lang="scss">
	.goods-detail-container {
		padding-bottom: 50px;
	}

	swiper {
		height: 750rpx;

		image {
			width: 100%;
			height: 100%;
		}
	}

	.goods-info-box {
		padding: 10px;
		padding-right: 0;

		.price {
			color: #c00000;
			font-size: 18px;
			margin: 10px 0;
		}

		.goods-info-body {
			display: flex;
			justify-content: space-between;

			.goods-name {
				font-size: 13px;
				padding-right: 10px;
			}

			// 收藏区域
			.favi {
				width: 120px;
				font-size: 12px;
				display: flex;
				flex-direction: column;
				justify-content: center;
				align-items: center;
				border-left: 1px solid #efefef;
				color: gray;
			}
		}

		// 运费
		.yf {
			margin: 10px 0;
			font-size: 12px;
			color: gray;
		}
	}

	.example-body {
		padding: 0;
		/* #ifndef APP-NVUE */
		display: block;
		/* #endif */
	}

	.goods-carts {
		/* #ifndef APP-NVUE */
		display: flex;
		/* #endif */
		flex-direction: column;
		position: fixed;
		left: 0;
		right: 0;
		/* #ifdef H5 */
		left: var(--window-left);
		right: var(--window-right);
		/* #endif */
		bottom: 0;
	}
</style>